const mongoose = require('mongoose')

const apiSchema = new mongoose.Schema({
  title: String, // api名称
  desc: String, // api描述
  method: String, // 请求方式
  path: String,
  type: String,
  parent: {
    // 所属组
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Api'
  } // 自关联,api分组
})

// api分组详细,指组成员
apiSchema.virtual('children', {
  localField: '_id',
  ref: 'Api',
  foreignField: 'parent',
  justOne: false
})

apiSchema.set('toObject', { virtuals: true })
apiSchema.set('toJSON', { virtuals: true })

const Api = mongoose.model('Api', apiSchema)

module.exports = Api
